package com.werdior.shiro.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.werdior.common.util.Constant;
import com.werdior.common.util.R;
import com.werdior.shiro.entity.Role;
import com.werdior.shiro.entity.User;
import com.werdior.shiro.entity.UserRole;
import com.werdior.shiro.service.IRoleService;
import com.werdior.shiro.service.IUserRoleService;
import com.xiaoleilu.hutool.convert.Convert;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * role 角色表 前端控制器
 * </p>
 *
 * @author werdior
 * @since 2017-12-23
 */
@Controller
@RequestMapping("/shiro/role")
public class RoleController  extends  AbstractController {

    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IUserRoleService iUserRoleService;

    /**
     * 获取角色列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/getList")
    public Map<String, Object> getUserList(@RequestParam Map<String, Object> params) {
        Map<String,Object> result = new HashMap<String,Object>();
        EntityWrapper<Role> wrapper = new EntityWrapper<Role>();
        //如果不是超级管理员，则只查询自己创建的角色列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("createUserId", getUserId());
            wrapper.where("create_user_id={0}", getUserId());
        }
        if (Convert.toBool(params.get("ispage"))==true){
            if (params.containsKey("name")){
                wrapper.like("name",params.get("name").toString());
            }
            wrapper.orderBy("sort", true);
            Page<Role> rolePage = iRoleService.selectPage(new Page<>(Convert.toInt(params.get("page")), Convert.toInt(params.get("limit"))), wrapper);
            result.put("count", iRoleService.selectCount(wrapper));
            result.put("data", rolePage.getRecords());
        }
        else {
            EntityWrapper<Role> ew=new EntityWrapper<>();
            result.put("roleList",iRoleService.selectList(ew));
        }
        return R.ok(result);
    }
    /**
     * 角色列表
     */
    @ResponseBody
    @RequestMapping("/select")
//    @RequiresPermissions("sys:role:select")
    public R select() {
        Map<String, Object> map = new HashMap<>();
        EntityWrapper<Role> wrapper = new EntityWrapper<>();
        //如果不是超级管理员，则只查询自己所拥有的角色列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            map.put("createId", getUserId());
            wrapper.where("create_id={0}", getUserId());
        }
        List<Role> list = iRoleService.selectList(wrapper);
        return R.ok().put("list", list);
    }
    /**
     * 添加角色
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping("/addRole")
    public R addRole(Role role) {
        return iRoleService.insert(role) ? R.ok("添加成功") : R.error("添加失败");
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping("/updateRole")
    public R updateRole(Role role) {
        return iRoleService.updateById(role) ? R.ok("修改成功") : R.error("修改失败");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public R delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtils.isEmpty(id)) {
            return R.error("请选择数据");
        }
        return iRoleService.deleteRoleByIdAndPermission(id) ? R.ok("删除成功") : R.error("删除失败");
    }


    /**
     * 分配角色
     * 查询用户拥有的角色
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserRole")
    public R getUserRole(@RequestBody User user) {
        EntityWrapper<UserRole> wrapper = new EntityWrapper<>();
        wrapper.eq("uid", user.getId());
        List<UserRole> userRoles = iUserRoleService.selectList(wrapper);
        return R.ok().put("userRoles",userRoles);
    }

    /**
     * 分配用户角色
     * @return
     */
    @ResponseBody
    @RequestMapping("/modifyUserRole")
    public R modifyUserRole(@RequestBody List<UserRole> userRoles) {
        return iRoleService.modifyUserRole(userRoles) ? R.ok("分配成功"): R.error("分配失败");
    }


}
